//data.js
// 记得在router.js中配置


const express = require('express')
//创建服务器
const router = express.Router()
const tool = require('../tools')
const path = require('path')

const { queryData,
    insertData,
    updateData,
    updateCharacter,
    deleteData,
    queryCharacterSkills,
    insertCharacterSkills,
    deleteCharacterSkills,
    exportDatas
} = require('../dataBase/api')
const multer = require('multer')
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + tool.getDateFormat(new Date()) + path.extname(file.originalname))
    }
})
const upload = multer({ storage: storage })

router.get("/query", (req, res, next) => {
    queryData(req.query)
        .then(response => {
            res.send(response)
        })
})

router.post("/add_character", upload.fields([{ name: "avatar", maxCount: 1 }, { name: "sprites", maxCount: 1 }]), (req, res, next) => {
    var { name, title, description, attributes, avatar, sprites } = req.body
    avatar = req.files["avatar"][0].path
    sprites = req.files["sprites"][0].path
    insertData([name, title, description, attributes, avatar, sprites], "character")
        .then(response => {
            res.send(response)
        })
})
router.post("/add", upload.single("icon"), (req, res, next) => {
    var { name, description, attributes, icon } = req.body
    icon = req.file.path
    insertData([name, description, attributes, icon], req.query.type)
        .then(response => {
            res.send(response)
        })
})

router.post("/update_character", upload.fields([{ name: "avatar", maxCount: 1 }, { name: "sprites", maxCount: 1 }]), (req, res, next) => {
    var { type, name, title, description, attributes, id } = req.body
    let avatar = req.body.text_avatar
    let sprites = req.body.text_sprites
    if (req.files["avatar"])
        avatar = req.files["avatar"][0].path
    if (req.files["sprites"])
        sprites = req.files["sprites"][0].path
    type = req.query.type
    id = req.query.id
    updateCharacter(type, id, [name, title, description, attributes, avatar, sprites])
        .then(response => {
            res.send(response)
        })
})

router.post("/update", upload.single("icon"), (req, res, next) => {
    var { type, name, description, attributes, id } = req.body
    let icon = req.body.text_icon
    if (req.file)
        icon = req.file.path
    type = req.query.type
    id = req.query.id
    updateData(type, id, [name, description, attributes, icon])
        .then(response => {
            res.send(response)
        })
})

router.get("/delete", (req, res, next) => {
    let id = req.query.id
    let type = req.query.type
    deleteData(id, type)
        .then(response => {
            res.send(response)
        })
})

router.get("/get_character_skills", (req, res, next) => {
    queryCharacterSkills()
        .then(response => {
            res.send(response)
        })
})


router.post("/add_character_skills", (req, res, next) => {
    var { character_id, skill_id } = req.body
    insertCharacterSkills([character_id, skill_id], req.query.type)
        .then(response => {
            res.send(response)
        })
})

router.get("/del_character_skills", (req, res, next) => {
    deleteCharacterSkills(req.query.id)
        .then(response => {
            res.send(response)
        })
})

router.post("/export_datas", (req, res, next) => {
    exportDatas()
        .then(response => {
            res.send(response)
        })
})
module.exports = router
